package com.web.admin.web.base.auth;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.http.message.ViewBuilder;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.facade.MenuAuthFacade;
import com.web.base.auth.facade.MenuFacade;
import com.web.base.auth.message.MenuAuthRequest;
import com.web.base.auth.message.MenuAuthResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("authMenuAuthController")
@Http(url = "/base/auth")
public class MenuAuthController
{
	@Autowired
	private MenuAuthFacade menuAuthFacade;
	@Autowired
	private MenuFacade menuFacade;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public MenuAuthController()
	{
	}
	
	@Http(url = "/menu_auth_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		MenuAuthRequest menuAuthRequest = request.getQueryObject(MenuAuthRequest.class);
		ViewBuilder builder = View.createBuilder();
		if (menuAuthRequest.getMenuId() != null)
		{
			builder.put("parentModel", menuFacade.get(menuAuthRequest.getMenuId()));
		}
		return builder.setView("/base/auth/menu_auth_list").put("model", menuAuthRequest)
					.put("map", dictParser.getStringMap())
					.put("modelList", menuAuthFacade.list(menuAuthRequest))
					.build();
	}
	
	@Http(url = "/menu_auth_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		MenuAuthRequest menuAuthRequest = request.getQueryObject(MenuAuthRequest.class);
		MenuAuthResponse response = null;
		if (menuAuthRequest.getId() != null)
		{
			response = menuAuthFacade.get(menuAuthRequest.getId());
		}
		else
		{
			response = new MenuAuthResponse();
			response.setMenuId(menuAuthRequest.getMenuId());
		}
		ViewBuilder builder = View.createBuilder();
		if (response.getMenuId() != null)
		{
			builder.put("parentModel", menuFacade.get(response.getMenuId()));
		}
		return builder.setView("/base/auth/menu_auth_get").put("model", response)
					.put("map", dictParser.getStringMap())
					.build();
	}
	
	@Http(url = "/menu_auth_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		MenuAuthRequest menuAuthRequest = request.getPostObject(MenuAuthRequest.class);
		MenuAuthResponse response = null;
		if (menuAuthRequest.getId() == null)
		{
			ValidationUtil.valid(menuAuthRequest, SaveModel.class);
			response = menuAuthFacade.add(menuAuthRequest);
		}
		else
		{
			ValidationUtil.valid(menuAuthRequest, UpdateModel.class);
			response = menuAuthFacade.update(menuAuthRequest);
		}
	}
	
	@Http(url = "/menu_auth_delete", method = HttpMethod.POST)
	public void delete(HttpRequest request)
	{
		int id = request.getPostInt("id", 0);
		menuAuthFacade.delete(id);
	}
	
}
